package nl.hro.mgdd.team4.raidersofhexagoniland.Scenes;

import java.io.IOException;
import java.io.InputStream;

import nl.hro.mgdd.team4.raidersofhexagoniland.GameActivity;

import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.color.Color;
import org.andengine.util.debug.Debug;

import android.opengl.GLES20;

public class MainMenuScene extends MenuScene implements IOnMenuItemClickListener {

	private ITexture _campaignMode;
	private ITextureRegion _campaignModeRegion;
	private ITexture _quit;
	private ITextureRegion _quitRegion;
	
	protected static final int CAMPAIGN_MODE = 0;
	protected static final int EXIT = CAMPAIGN_MODE + 1;

	public MainMenuScene(){
		this.setCamera(GameActivity.getCamera());
		setBackground(new Background(Color.BLACK));
		setBackgroundEnabled(true);
		
		try{
			SceneManager.getManager();
			_campaignMode = new BitmapTexture(SceneManager._game.getTextureManager(), new IInputStreamOpener(){

				@Override
				public InputStream open() throws IOException {
					SceneManager.getManager();
					return SceneManager._game.getAssets().open("gfx/menu_cmp.png");
				}
			});
			_campaignMode.load();
			_campaignModeRegion = TextureRegionFactory.extractFromTexture(_campaignMode);
			
			_quit = new BitmapTexture(SceneManager._game.getTextureManager(), new IInputStreamOpener() {
				
				@Override
				public InputStream open() throws IOException {
					SceneManager.getManager();
					return SceneManager._game.getAssets().open("gfx/menu_quit.png");
				}
			});
			_quit.load();
			_quitRegion = TextureRegionFactory.extractFromTexture(_quit);
		
		} catch (IOException e) {
			Debug.e(e);
		}
		
		final SpriteMenuItem cmMenuItem = new SpriteMenuItem(CAMPAIGN_MODE, _campaignModeRegion, SceneManager._game.getVertexBufferObjectManager());
		cmMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(cmMenuItem);
		final SpriteMenuItem qMenuItem = new SpriteMenuItem(EXIT, _quitRegion, SceneManager._game.getVertexBufferObjectManager());
		qMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(qMenuItem);
		
		buildAnimations();
		setOnMenuItemClickListener(this);

//			face.dispose();
//			qButton.dispose();
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch(pMenuItem.getID()){
		case CAMPAIGN_MODE:
			GameActivity._manager.setWorld1Level1();
			return true;
		case EXIT:
			SceneManager._game.finish();
			return true;
		default:
			return false;
		}
	}
}
